package com.xian.java;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class MyDataUtil {

    public  static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式
    public  static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");//要转换的时间格式


    public static String getYYYYMMDD(){
        String mydate="YYYYMMDD";
        Date date = new Date();
        Instant instant = date.toInstant();
        ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
        LocalDateTime localDateTime1 = zonedDateTime.toLocalDateTime();
        //System.out.println(localDateTime1.toString());
        //System.out.println(localDateTime1.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        //System.out.println(localDateTime1.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSS")));
        mydate = localDateTime1.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        return mydate;
    }


    //时间戳转
    public static String timeStamp2Date(String time) {
        String mydate = "timeStampXXX";
        Long timeLong = Long.parseLong(time);
        Date date;
        try {
            date = sdf2.parse(sdf2.format(timeLong));
            mydate = sdf2.format(date);
            return mydate;
        } catch (Exception e) {
            e.printStackTrace();
            return mydate;
        }

    }

}
